# 序列化数据集对象
from collections import Iterable


def serialize_models(modelsObj):
    try:
        # 可迭代对象是转换成列表，否则转换成字典
        if isinstance(modelsObj, Iterable):
            # 序列化成列表格式
            # 清洗列表数据(列表推导式)
            t_modelsObj = []
            for item in modelsObj:
                t_modelsObj.append({key: value for key, value in item.__dict__.items ()  if key != '_sa_instance_state' })
            modelsObj = list.copy(t_modelsObj)
        else:
            modelsObj = {key: value for key, value in modelsObj.__dict__.items () if key != '_sa_instance_state' }
        return modelsObj
    except Exception as e:
        print("序列化数据集对象异常",e)
        return None